/*
 * @Description: 电子文件移交归档reducer
 * @Author: Rfan
 * @Date: 2022-11-19 16:31:39
 * @LastEditTime: 2022-11-19 16:39:44
 */

import {
  DELETE_REGISTRATION_ITEM,
  SET_SELECTED_ITEM,
  ADD_REG_ARCHIVING,
  EDIT_REG_ARCHIVING,
  SET_REG_ARCHIVING_LIST
} from '../constants';
import { delItemByIdArray, editItemById, insertItemAfter } from '@/utils/list';

export const ArchivingReducer = (state, action) => {
  switch (action.type) {
    /**
     * @description: 保存表格选中的条目id
     * @param {number[]} data
     */
    case SET_SELECTED_ITEM:
      return { ...state, selectItems: action.data };
    /**
     * @description: 设置电子文件移交归档登记列表
     */
    case SET_REG_ARCHIVING_LIST: {
      return {
        ...state,
        list: action.data
      };
    }

    /**
     * @description: 添加电子文件移交归档登记条目
     */
    case ADD_REG_ARCHIVING: {
      return {
        ...state,
        list: insertItemAfter(action.data, state.list)
      };
    }

    /**
     * @description: 编辑电子文件移交归档登记条目
     */
    case EDIT_REG_ARCHIVING: {
      const { id, data } = action;
      return {
        ...state,
        list: editItemById(id, data, state.list)
      };
    }

    /**
     * @description: 删除待处置列表项目
     */
    case DELETE_REGISTRATION_ITEM: {
      return {
        ...state,
        list: delItemByIdArray(action.ids, state.list)
      };
    }

    default:
      console.log('untouched', action);
      return state;
  }
};
